<template>
	<view>
		<u-popup :show="show" :closeOnClickOverlay="false" mode="center" @close="close(false)">
			<view class="popupContent">
				<view class="select-box">
					<title title="请选择机巢"></title>
					<view class="select-data" @click="selectShow=!selectShow">
						<text>{{machineItem.name===""?initName:machineItem.name}}</text>
						<u-icon :name="selectShow?'arrow-up-fill':'arrow-down-fill'"></u-icon>
						<view class="machine-list" v-show="selectShow">
							<view @click="changeMachineName(item)" class="machine-item" v-for="item in machineList"
								:key="item.ID">
								{{item.nestName}}
							</view>
						</view>
					</view>
					<view class="btns">
						<text @click="cancel">取消</text>
						<text @click="confirm">确认</text>
					</view>
				</view>

			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex"
	export default {
		props: {
			show: {
				type: Boolean,
				default: false,
			}
		},
		data() {
			return {
				selectShow: false,
				machineItem: {
					name: '',
					id: -1
				},

			}
		},
		watch: {
			show(n) {
				this.machineItem = {
					name: '',
					id: -1
				}
			}
		},
		computed: {
			...mapState(["machineList"]),
			initName() {
				console.log(this.$store.state.mechineEvent.name, '12312')
				return this.$store.state.mechineEvent.name
			}
		},

		methods: {

			close(flat) {
				this.$emit("close", flat)
				// console.log('close');
			},
			cancel() {
				this.$emit("cancel")
			},
			confirm() {
				this.$emit("confirm", this.machineItem)
			},
			changeMachineName(item) {
				this.machineItem.name = item.nestName
				this.machineItem.id = item.ID
				this.machineItem.nestId = item.nestid
				console.log(this.machineItem.nestId)
			}
		}
	}
</script>

<style lang="scss">
	.popupContent {
		padding: 10rpx 40rpx 36rpx;
		box-sizing: border-box;

		.select-box {
			width: 400rpx;
			font-size: 26rpx;

			.select-data {
				border-radius: 10rpx;
				width: 100%;
				border: 1rpx solid $color-gray;
				height: 80rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				justify-content: space-between;
				position: relative;

				.machine-list {
					max-height: 300rpx;
					width: 100%;
					background-color: white;
					overflow: scroll;
					position: absolute;
					top: 80rpx;
					left: 0;
					box-shadow: 0px 3px 8px 0px #00000040;
					border-radius: 10rpx;

					.machine-item {
						margin: 30rpx 20rpx;
						font-size: 26rpx;

						&:first-child,
						&:last-child {
							margin: 20rpx;
						}

					}
				}
			}

			.btns {
				margin-top: 50rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: white;

				>text {
					width: 160rpx;
					height: 66rpx;
					display: flex;
					align-items: center;
					justify-content: center;

					&:first-child {
						background-color: $color-blue;
						border-radius: 10rpx
					}

					&:last-child {
						background-color: $color-green;
						border-radius: 10rpx
					}
				}
			}
		}

	}
</style>